﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using Framework.Common;
using Mode;

namespace WebSite.WebServer
{
    public partial class reflush : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

            //修改
            int userid = 0;
            string uUserid = Request.QueryString["userid"];
            Int32.TryParse(uUserid, out userid);
            Application.Lock();
            if (selectItem(userid) > 0)
            {
                //删除用户
                DelTimeOut();
            }
            else
            {
               
                DataTable dtt = (DataTable)Application["onLineUser"];
              
                loginInfo login = SessionShard.loginUser;
                if (login != null)
                {
                    if (login.M_NAME != "")
                    {
                        DataRow dr = dtt.NewRow();
                        dr["Userid"] = login.M_ID.ToString();
                        dr["UserName"] = login.M_NAME;
                        dr["Phone"] = login.M_PHONE;
                        dr["A_Id"] = login.A_ID.ToString();
                        dr["A_Name"] = login.A_NAME;
                        dr["R_Id"] = login.R_ID.ToString();
                        dr["R_Name"] = login.R_NAME;
                        dr["J_Id"] = login.J_ID.ToString();
                        dr["RefreshTime"] = DateTime.Now;

                        dtt.Rows.Add(dr);


                        dtt.AcceptChanges();
                    }
                }
               
            }
            Application.UnLock();

            
        

         



            Response.Write("{\"msg\":\""+DateTime.Now.ToString("HH:mm:ss")+"\"}");
            //Response.End();
            
            
        }

        /// <summary>
        ///清除超时用户。
        /// </summary>
        private void DelTimeOut()
        {
            DataTable dt = (DataTable)Application["onLineUser"];
            if (dt != null)
            {
                string strExpr = " RefreshTime < #" + DateTime.Now.AddMinutes(0 - 1) + "#";
                DataRow[] curUser = dt.Select(strExpr);
                if (curUser.Length > 0)
                {
                    for (int i = 0; i < curUser.Length; i++)
                    {
                        curUser[i].Delete();
                    }
                }
                dt.AcceptChanges();
            }
        }

        private int selectItem(int userid)
        {
            DataTable dt = (DataTable)Application["onLineUser"];
            if (dt != null)
            {
                DataRow[] curUser = dt.Select("Userid=" + userid);
                if (curUser.Length > 0)
                {
                    for (int i = 0; i < curUser.Length; i++)
                    {
                        curUser[i]["RefreshTime"] = DateTime.Now;
                    }
                    dt.AcceptChanges();
                    return 1;
                }
                else
                    return 0;
            }
            else
            {


                return 0;
            }
        }

        private int isUsrItem(int userid)
        {

            return 0;
        }
            
    }


}


